﻿window.top.UIResource = UIResource;
function loadGlobalResourceLanguage(callback) {
    if (!window.top.$globalRes) {
        window.top.$globalRes = {};
    }
   
    if (!window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()]) {
        window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()] = {};
    }
    synGlobalResource(function () {
        var lang = ngW8.$("$wslang");

        var Keys = Object.keys(window.top.UIResource);
        var count = 0;

        for (var i = 0; i < Keys.length; i++) {

            lang.$loadByFormName(ngW8.$("$lang").$getLanguageCode(),  Keys[i], function (res) {
                angular.forEach(res, function (f) {
                    var k = f[0].split('.');
                    if (!window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()][k[0]]) {
                        window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()][k[0]] = {};
                    }
                    window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()][k[0]][k[1]] = f[1];

                   
                });
                count++;
                if (count == Keys.length) {
                    callback();
                }
            });

        }
    });
    
}
function synGlobalResource(loadComplete) {
    
    var lang = ngW8.$("$wslang");

    var Keys = Object.keys(window.top.UIResource);
    var count = 0;
    var sysList = [];
    for (var i = 0; i < Keys.length; i++) {
        
        var SubKeys = Object.keys(window.top.UIResource[Keys[i]]);
        for (var j = 0; j < SubKeys.length; j++) {
            /*
            public string FormName { get; set; }
             public string Key { get; set; }
         public string LanguageCode { get; set; }

         public string Caption { get; set; }

         public string Type { get; set; }

         public string LabelType { get; set; }
            */
            var resKey = Keys[i] + "." + SubKeys[j];
            var value = window.top.UIResource[Keys[i]][SubKeys[j]];
            sysList.push({
                Key: resKey,
                Caption: value,
                LabelType: Keys[i],
                FormName: Keys[i],
                LanguageCode: ngW8.$("$lang").$getLanguageCode()
            });
        }
    }
    if (ngW8.$("$wslang").$getUrl()) {
        $.ajax({
            type: "POST",
            url: ngW8.$("$wslang").$getUrl() + "/syn",
            data: JSON.stringify(sysList),
            success: function (res) {

                loadComplete(res);

            },
            error: function (error) {
                var cancel = false;
                if (window.top.$onCallWebServiceErrorHandler) {
                    cancel = window.top.$onCallWebServiceErrorHandler({
                        url: $(this)[0].url,
                        error: error
                    });
                }
                if (cancel) return;
                var div = $("<div><iframe></iframe></div>").appendTo("body");
                div.kendoWindow({
                    width: $(window).width() - 40,
                    height: $(window).height() - 40
                });
                div.data("kendoWindow").center()
                var iframe = div.children()[0];
                $(iframe).css("width", $(window).width() - 40);
                $(iframe).css("height", $(window).height() - 60);

                var safeData = window.toStaticHTML(error.responseText);
                iframe.contentWindow.document.open();
                iframe.contentWindow.document.write(safeData);
                iframe.contentWindow.document.close();

            },
            processData: true,
            dataType: "json",
            contentType: "application/json; charset=utf-8"
        });
    }
};
function applyGlobalResource(scope) {
    if (!window.top.$globalRes) {
        window.top.$globalRes = {};
    }
   
    scope.$gRes = window.top.$globalRes[ngW8.$("$lang").$getLanguageCode()];
}